var ns;
Vue.component('tree-item', {
    template: '#item-template',
    props: {
        item: {}
    },
    data: function() {
        return {
            isOpen: true,
            selecttypeid: app.TypeID
        }
    },
    computed: {
        isFolder: function() {
            return this.item.children &&
                this.item.children.length
        }
    },
    methods: {
        toggle: function(item) {
            if (this.isFolder) {
                this.isOpen = !this.isOpen
            } else {
                this.choose_type(item);
            }
        },
        choose_type: function(item) {
            var that = this;
            app.TypeID = item.ID;
            app.TypeName = item.text;
            setTimeout(function() {
                app.is_type_list = false;
            }, 100)
        },
    }
})
var app = new Vue({
    el: '#app',
    data: {
        list: [],
        ServiceType: 1, //1-客服工单 2-投诉建议 3-品控管理
        //1-派单 2-工单转移 3-工单协同
        PublishType: 1,
        ID: 0,
        select_id_list: [],
        is_xietong: true,
        is_type_list: false,
        is_tag_list: false,
        is_category_list: false,
        typelist: [],
        TypeID: 0,
        TypeName: '请选择',
        taglist: [],
        TagID: 0,
        TagName: '请选择',
        categorylist: [],
        CategoryID: 0,
        CategoryName: '请选择',
    },
    methods: {
        get_data: function() {
            var that = this;
            ns.post({
                keywords: that.keywords,
                ID: that.ID,
                action: 'getorgnizationlist',
                ServiceType: that.ServiceType
            }, function(succeed, data, err) {
                if (succeed) {
                    that.list = data.list;
                    that.typelist = data.typelist;
                    that.TypeID = data.TypeID;
                    that.TypeName = data.TypeName;
                    that.taglist = data.taglist;
                    that.TagID = data.TagID;
                    that.TagName = data.TagName;
                    that.categorylist = data.categorylist;
                    that.CategoryID = data.CategoryID;
                    that.CategoryName = data.CategoryName;
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        do_select_publishtype: function(type) {
            this.PublishType = type;
        },
        choose_tag: function(item) {
            var that = this;
            that.TagID = item.ID;
            that.TagName = item.Name;
            that.is_tag_list = false;
        },
        choose_category: function(item) {
            var that = this;
            that.CategoryID = item.ID;
            that.CategoryName = item.Name;
            that.is_category_list = false;
        },
        tel_phone: function(item) {
            api.call({
                type: 'tel_prompt',
                number: item.phone
            });
        },
        open_type_list: function() {
            var that = this;
            that.is_tag_list = false;
            that.is_category_list = false;
            that.is_type_list = !that.is_type_list;
        },
        open_tag_list: function() {
            var that = this;
            that.is_type_list = false;
            that.is_tag_list = !that.is_tag_list;
            that.is_category_list = false;
        },
        open_category_list: function() {
            var that = this;
            that.is_type_list = false;
            that.is_tag_list = false;
            that.is_category_list = !that.is_category_list;
        },
        do_choose: function(item, list) {
            item.isselected = !item.isselected;
        },
        set_select_items: function() {
            var that = this;
            that.select_id_list = [];
            for (var i = 0; i < that.list.length; i++) {
                var item = that.list[i];
                if (item.TypeID == 4) {
                    if (item.isselected) {
                        that.select_id_list.push(item.ID);
                    }
                    continue;
                }
                if (!item.children) {
                    continue;
                }
                for (var j = 0; j < item.children.length; j++) {
                    var item2 = item.children[j];
                    if (item2.TypeID == 4) {
                        if (item2.isselected) {
                            that.select_id_list.push(item2.ID);
                        }
                        continue;
                    }
                    if (!item2.children) {
                        continue;
                    }
                    for (var k = 0; k < item2.children.length; k++) {
                        var item3 = item2.children[k];
                        if (item3.isselected) {
                            that.select_id_list.push(item3.ID);
                        }
                    }
                }
            }
        },
        do_save: function() {
            var that = this;
            that.set_select_items();
            if (that.select_id_list.length == 0) {
                api.toast({
                    msg: '请选择服务人员',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.PublishType == 1 && that.CategoryID == 0 && that.ServiceType == 3) {
                var toasttitle = '必须选择问题分类'
                api.toast({
                    msg: toasttitle,
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.PublishType == 1 && that.TypeID == 0 && (that.ServiceType == 1 || that.ServiceType == 2)) {
                var toasttitle = '必须选择工单类型'
                api.toast({
                    msg: toasttitle,
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            var options = {};
            options.action = "dopublicservice";
            options.ID = that.ID;
            options.PublishType = that.PublishType; // 1派单 2转交 3协同
            options.ServiceType = that.ServiceType; // 1客服任务 2-品控管理
            options.TypeID = that.TypeID;
            options.TagID = that.TagID;
            options.AcceptUserIDs = JSON.stringify(that.select_id_list);
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    api.toast({
                        msg: '操作成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                    api.sendEvent({
                        name: 'updata_task_con',
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                setTimeout(function() {
                    api.closeWin();
                }, 500)
            }, {
                toast: true,
                toastmsg: '提交中'
            });
            return;
        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.ID = api.pageParam.id || 0;
    app.PublishType = api.pageParam.type || 0;
    app.ServiceType = api.pageParam.ServiceType || 1;
    app.is_xietong = api.pageParam.is_xietong || false;
    app.get_data();
};
